#include "user.h"
#include "thread_pool.h"
#include <stdio.h>
#include <string.h>
#include <shadow.h>


static void get_setting(char* setting,char* passwd){
    int i,j;
    //取出salt ,i ,记录密码字符下表，j记录$出现次数
    for(i =0,j=0;passwd[i] &&j!=4;++i){
        if(passwd[i]=='$') ++j;
    }
    strncpy(setting ,passwd,i);
}
//提取设置信息发送给客户端，为后续密码验证作准备
void loginCheck1(user_info_t* user){
    printf("loginCheck1.\n");
    train_t t;
    int ret;
    memset(&t,0,sizeof(t));
    struct spwd* sp= getspnam(user->name); //读取影子密码文件的函数,查询用户的密码信息
    if(sp==NULL){//用户不存在情况下
        t.len=0;
        t.type =TASK_LOGIN_SECTION1_RESP_ERROR;
        ret = sendn(user->sockfd,&t,8);
        printf("check1 send %d bytes.\n",ret);
        return ;
    }

    //用户存在情况下
    char setting[100]={0};
    //保密加密密文
    strcpy(user->encrypted,sp->sp_pwdp);

    //提取seting
    get_setting(setting,sp->sp_pwdp);
    t.len = strlen(setting);
    t.type = TASK_LOGIN_SECTION1_RESP_OK;
    strncpy(t.buff,setting,t.len);
    //发送setting
    ret = sendn(user->sockfd,&t,8+t.len); //加密结果发送给服务器
    printf("check1 send %d bytes.\n",ret);

}

//验证密码是否正确
void loginCheck2(user_info_t* user,const char * encrypted){
    int ret;
    train_t t;
    memset(&t,0,sizeof(t));
    if(strcmp(user->encrypted,encrypted)==0){  //比较加密密码
        //登陆成功
        user->status = STATUS_LOGIN; //更新用户登陆成功状态
        t.type = TASK_LOGIN_SECTION2_RESP_OK;
        t.len = strlen("/server/$"); //将暂定将 /server/ 作为pwd传递给client 
        strcpy(t.buff,"/server/$");
        ret =sendn(user->sockfd,&t,8+t.len);
        printf("Login success.\n");

    }else{
        //登陆失败
        t.type =TASK_LOGIN_SECTION2_RESP_ERROR;
        printf("Login failed.\n");
        ret = sendn(user->sockfd,&t,8);
    }
    printf("check2 send %d bytes.\n",ret);
    return ;
}
